<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GroupGood;
use App\Admin\Server\GroupService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Redis;

class GroupGoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GroupGood(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('image','拼团商品图片')->image();
            $grid->column('group_name','拼团商品名称');
            $grid->column('original_cost','原价');
            $grid->column('group_price','拼团价格');
            $grid->column('group_number','拼团人数');
            $grid->column('people','参与人数');
            $grid->column('clustering_num','成团数');
            $grid->column('limit_quantity','限量');
            $grid->column('residue_quantity','限量剩余');
            $grid->column('duration','拼团时间');
            $grid->column('group_state', '拼团状态')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GroupGood(), function (Show $show) {
            $show->field('id');
            $show->field('image');
            $show->field('group_name');
            $show->field('original_cost');
            $show->field('group_price');
            $show->field('group_number');
            $show->field('people');
            $show->field('clustering_num');
            $show->field('limit_quantity');
            $show->field('residue_quantity');
            $show->field('stop_time');
            $show->field('group_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GroupGood(), function (Form $form) {
            $form->display('id');
            $form->selectTable('goods_id')->rules('required')->from(GroupService::make())->saving(function ($e){
                return $e[0];
            });
            $form->image('image','拼团商品图片')->uniqueName()->saveFullUrl();
            $form->text('group_name','拼团商品名称');
            $form->decimal('original_cost','原价');
            $form->decimal('group_price','拼团价格');
            $form->text('group_number','拼团人数');
            $form->text('clustering_num','成团数量');
            $form->text('limit_quantity','限量');
            $form->text('duration','拼团时间/小时')->value(24);
            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function (Form $form){
                //判断是否是新增操作
                if($form->isCreating()){
                    //获取到限量的商品数
                    $limit_quantity = $form->limit_quantity;
                    //获取拼团人数
                    $group_number = $form->group_number;
                    //获取到一共可以拼几个团
                    $group = $limit_quantity/$group_number;
                    //然后判断成团数是否大于得到的团数
                    if($form->clustering_num>$group){
                        return $form->response()->error("成团数过大");
                    }
                }
            });
        });
    }
}
